package com.yhs.YShop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yhs.YShop.elastice.service.CommodityService;
import com.yhs.YShop.model.Commodity;


@RequestMapping("commodity")
@RestController
public class CommodityController {

	@Autowired
	private CommodityService commodityService;

	@RequestMapping("insertCommodity")
	public String insertCommodity() {
		Commodity commodity = new Commodity();

		commodity.setBrand("brands");
		commodity.setCategory("分类");
		commodity.setName("衣服");
		commodity.setPrice(150);
		commodity.setSkuId("63356");
		commodity.setStock(560);
		Commodity commodity2 = commodityService.save(commodity);

		return commodity2.getName();
	}
	
	@RequestMapping("findAll")
	public Map<String, Object> findAll()
	{
		Map<String, Object> resuleMap = new HashMap<String,Object>();
		Iterable<Commodity> commodities =  commodityService.getAll();
		List<Commodity> commodities2 = new ArrayList<Commodity>();
		commodities.forEach(s -> {commodities2.add(s);});
	    resuleMap.put("elasticsearch", commodities2);
	    return resuleMap;
	}
}
